package com.yunjinsuke.recharge.scheduled;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;

/**
 * 异步定时任务配置类
 * <p>
 * 作者：wqh3520
 * 来源：CSDN
 * 原文：https://blog.csdn.net/wqh8522/article/details/79224290
 * 版权声明：本文为博主原创文章，转载请附上博文链接！
 */
@Configuration
// 开启定时任务
@EnableScheduling
// 异步执行任务,必须配合@Async使用
@EnableAsync
public class AsyncScheduledConfig {

    // 核心线程池大小
    private static final int corePoolSize = 10;
    // 最大线程池大小
    private static final int maxPoolSize = 20;
    // 队列大小
    private static final int queueCapacity = 10;

    @Bean
    public Executor taskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(corePoolSize);
        executor.setMaxPoolSize(maxPoolSize);
        executor.setQueueCapacity(queueCapacity);
        executor.initialize();
        return executor;
    }
}
